#include <asm/biosdef.h>

// os size location (os_size could be sectors [p1-task3] or bytes [p1-task4])
.equ os_size_loc, 0x502001fc

// kernel address (move kernel to here, and jmp here to start kernel)
.equ kernel, 0x50201000

// BIOS function entry (jump here to use BIOS APIs)
.equ bios_func_entry, 0x50150000

// taskinfo location in memory
.equ taskinfo, 0x52010000
.equ offset_num, 0x52000000

.text
.global main

main:
	// fence on all memory and I/O
	fence

	// TODO: [p1-task1] call BIOS to print string "It's bootblock!"
        li a0, BIOS_PUTSTR
        la a1, msg
        call bios_func_entry


	// TODO: [p1-task2] call BIOS to read kernel in SD card
        li a0, BIOS_SDREAD
        la a1, kernel
        la a2, os_size_loc
        lh a2, 0(a2)
        li a3, 1
        call bios_func_entry

	// TODO: [p1-task4] load task-related arguments and pass them to kernel
        li a0, BIOS_SDREAD
        la a1, taskinfo
        la a2, os_size_loc
        addi a2, a2, -2
        addi a3, a2, -2
        lh a2, 0(a2)
        lh a3, 0(a3)
        call bios_func_entry
        
        //pass the argument to main.c
        la a0, offset_num
        la a2, os_size_loc
        lw a2, -8(a2)
        sw a2, 0(a0)

	// TODO: [p1-task2] jump to kernel to start UCAS-OS
        call kernel      

  
// while(1) --> stop here
stop:
	j stop
	nop

.data

msg: .string "It's a bootloader...\n\r"
debug: .string "debug...\n\r"
